package com.nealin.shardingjdbctable.controller;

import com.nealin.shardingjdbctable.entity.Order;
import com.nealin.shardingjdbctable.entity.OrderItem;
import com.nealin.shardingjdbctable.mapper.OrderItemMapper;
import com.nealin.shardingjdbctable.mapper.OrderMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单表(Order)表控制层
 *
 * @author HuangBingzhi
 * @since 2020-04-12 23:00:56
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper itemMapper;

    @GetMapping("insert")
    public int insert(Long id){
        Order order=new Order();
        order.setOrderName("电视机")
                .setCreateTime(new Date())
                    .setCreateUser(2L);
        orderMapper.insertNoId(order);
        return 1;
    }
    @GetMapping("update")
    public int update(Long id){
        Order order=new Order();
        order.setOrderId(id)
                .setOrderName("电视机1")
                .setCreateTime(new Date());
        return orderMapper.update(order);
    }
    @GetMapping("getById")
    public Order getById(Long id){
        return orderMapper.queryById(id);
    }
    @GetMapping("deleteById")
    public int deleteById(Long id){
        return orderMapper.deleteById(id);
    }
    @PostMapping("getByName")
    public List<Order> getByName(@RequestBody Order order){
        return orderMapper.queryAll(order);
    }

    @GetMapping("insertOrderAndItem")
    public void insertOrderAndItem(){
        for(int i=0;i<10;i++){
            Order order=new Order();
            order.setOrderName("电视机"+i)
                    .setCreateTime(new Date())
                    .setCreateUser(2L);
            orderMapper.insertNoId(order);
            OrderItem item =new OrderItem();
            item.setOrderId(order.getOrderId());
            item.setItemName("电源"+i);
            item.setCreateUser(2L);
            item.setCreateTime(new Date());
            itemMapper.insert(item);
        }

    }
    @PostMapping("getOrderAndItem")
    public List<Order> getOrderAndItem(@RequestBody Order order){
        List<Order> orders=orderMapper.getOrderAndItem(order);
        return orders;
    }
}